/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.builtin;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.inspur.edp.cef.api.manager.serialize.CefSerializeContext;
import com.inspur.edp.cef.entity.entity.FieldType;
import com.inspur.edp.cef.spi.entity.PropertyDefaultValue;
import com.inspur.edp.cef.spi.entity.info.propertyinfo.BasePropertyInfo;
import com.inspur.edp.cef.spi.entity.info.propertyinfo.DataTypePropertyInfo;
import com.inspur.edp.cef.spi.entity.info.propertyinfo.ObjectType;
import com.inspur.edp.cef.spi.jsonser.util.SerializerUtil;

public class BffPropertyInfo extends DataTypePropertyInfo {

    private String beLabelId;
    private boolean enableStdTimeFormat;

    public BffPropertyInfo(String propertyName, String displayValueKey, boolean required, boolean enableRtrim, int length, int precision, FieldType fieldType, ObjectType objectType, BasePropertyInfo objectInfo, PropertyDefaultValue defaultValue) {
        super(propertyName, displayValueKey, required, enableRtrim, length, precision, fieldType, objectType, objectInfo, defaultValue);
    }

    public BffPropertyInfo(String propertyName, String displayValueKey, boolean required, boolean enableRtrim, int length, int precision, FieldType fieldType, ObjectType objectType, BasePropertyInfo objectInfo, PropertyDefaultValue defaultValue, boolean isMulti) {
        super(propertyName, displayValueKey, required, enableRtrim, length, precision, fieldType, objectType, objectInfo, defaultValue, isMulti);
    }

    public BffPropertyInfo(String propertyName, String displayValueKey, boolean required, boolean enableRtrim, int length, int precision, FieldType fieldType, ObjectType objectType, BasePropertyInfo objectInfo, PropertyDefaultValue defaultValue, boolean isMulti, boolean bigNumber) {
        super(propertyName, displayValueKey, required, enableRtrim, length, precision, fieldType, objectType, objectInfo, defaultValue, isMulti, bigNumber);
    }

    public BffPropertyInfo(String propertyName, String displayValueKey, boolean required, boolean enableRtrim, int length, int precision, FieldType fieldType, ObjectType objectType, BasePropertyInfo objectInfo, PropertyDefaultValue defaultValue, boolean isMulti, boolean bigNumber, String beLabelId) {
        super(propertyName, displayValueKey, required, enableRtrim, length, precision, fieldType, objectType, objectInfo, defaultValue, isMulti, bigNumber);
        this.beLabelId = beLabelId;
    }

    public BffPropertyInfo(String propertyName, String displayValueKey, boolean required, boolean enableRtrim, int length, int precision, FieldType fieldType, ObjectType objectType, BasePropertyInfo objectInfo, PropertyDefaultValue defaultValue, boolean isMulti, String beLabelId) {
        super(propertyName, displayValueKey, required, enableRtrim, length, precision, fieldType, objectType, objectInfo, defaultValue, isMulti);
        this.beLabelId = beLabelId;
    }

    public BffPropertyInfo(String propertyName, String displayValueKey, boolean required, boolean enableRtrim, int length, int precision, FieldType fieldType, ObjectType objectType, BasePropertyInfo objectInfo, PropertyDefaultValue defaultValue, boolean isMulti, boolean bigNumber, String beLabelId, boolean enableStdTimeFormat) {
        super(propertyName, displayValueKey, required, enableRtrim, length, precision, fieldType, objectType, objectInfo, defaultValue, isMulti, bigNumber);
        this.beLabelId = beLabelId;
        this.enableStdTimeFormat = enableStdTimeFormat;
    }

    public String getBeLabelId() {
        return this.beLabelId;
    }

    public boolean isEnableStdTimeFormat() {
        return enableStdTimeFormat;
    }

    private CefSerializeContext getCefSerializeContext(CefSerializeContext serContext) {
        if (serContext == null)
            serContext = new CefSerializeContext();
        return serContext;
    }

    //
    public void write(JsonGenerator writer, Object value, SerializerProvider serializer, CefSerializeContext serContext) {
        serContext = getCefSerializeContext(serContext);

        if (enableStdTimeFormat) {
            serContext.setEnableStdTimeFormat(enableStdTimeFormat);
        }
        super.write(writer, value, serializer, serContext);
    }

    public Object read(JsonParser reader, DeserializationContext serializer, CefSerializeContext serContext) {
        serContext = getCefSerializeContext(serContext);

        if (enableStdTimeFormat) {
            serContext.setEnableStdTimeFormat(enableStdTimeFormat);
        }
        return super.read(reader, serializer, serContext);
    }

    public void writeChange(JsonGenerator writer, Object value, SerializerProvider serializer, CefSerializeContext serContext) {
        serContext = getCefSerializeContext(serContext);

        if (enableStdTimeFormat) {
            serContext.setEnableStdTimeFormat(enableStdTimeFormat);
        }
        super.writeChange(writer, value, serializer, serContext);
    }

    public Object readChange(JsonParser reader, DeserializationContext serializer, CefSerializeContext serContext) {
        serContext = getCefSerializeContext(serContext);

        if (enableStdTimeFormat) {
            serContext.setEnableStdTimeFormat(enableStdTimeFormat);
        }
        return super.readChange(reader, serializer, serContext);
    }

    @Override
    protected final void writeDecimalProp(JsonGenerator writer, Object value, SerializerProvider serializer, CefSerializeContext serContext) {
        SerializerUtil.writeDecimal(writer, value, this.getPropertyName(), serializer, this.getIsBigNumber());
    }
}
